package edu.cuit.project.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;

import edu.cuit.project.domain.AddressArea;
import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * AddressArea entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see edu.cuit.project.domain.AddressArea
 * @author MyEclipse Persistence Tools
 */

public class AddressAreaDAO extends BaseHibernateDAO   {
	private static final Log log = LogFactory.getLog(AddressAreaDAO.class);
	// property constants
	public static final String AREA_ID = "areaId";
	public static final String AREA = "area";
	public static final String FATHER = "father";

	/* (non-Javadoc)
	 * @see edu.cuit.project.dao.IArea#save(edu.cuit.project.domain.AddressArea)
	 */
	
	public void save(AddressArea transientInstance) {
		log.debug("saving AddressArea instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(AddressArea persistentInstance) {
		log.debug("deleting AddressArea instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see edu.cuit.project.dao.IArea#findById(java.lang.Integer)
	 */
	
	public AddressArea findById(java.lang.Integer id) {
		log.debug("getting AddressArea instance with id: " + id);
		try {
			AddressArea instance = (AddressArea) getSession().get(
					"edu.cuit.project.domain.AddressArea", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<AddressArea> findByExample(AddressArea instance) {
		log.debug("finding AddressArea instance by example");
		try {
			List<AddressArea> results = (List<AddressArea>) getSession()
					.createCriteria("edu.cuit.project.domain.AddressArea")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding AddressArea instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from AddressArea as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<AddressArea> findByAreaId(Object areaId) {
		return findByProperty(AREA_ID, areaId);
	}

	public List<AddressArea> findByArea(Object area) {
		return findByProperty(AREA, area);
	}

	public List<AddressArea> findByFather(Object father) {
		return findByProperty(FATHER, father);
	}

	/* (non-Javadoc)
	 * @see edu.cuit.project.dao.IArea#findAll()
	 */
	public List findAll() {
		log.debug("finding all AddressArea instances");
		try {
			String queryString = "from AddressArea";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see edu.cuit.project.dao.IArea#merge(edu.cuit.project.domain.AddressArea)
	 */
	
	public AddressArea merge(AddressArea detachedInstance) {
		log.debug("merging AddressArea instance");
		try {
			AddressArea result = (AddressArea) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(AddressArea instance) {
		log.debug("attaching dirty AddressArea instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(AddressArea instance) {
		log.debug("attaching clean AddressArea instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}